package com.ruoyi.shbData.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shbData.domain.pad.DataShbModifyReqDTO;
import com.ruoyi.shbData.domain.pad.ShbChartRespDTO;
import com.ruoyi.shbData.domain.plat.DataShb;
import com.ruoyi.shbData.domain.report.ShbReportReqDTO;
import com.ruoyi.shbData.service.IDataShbService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 水泵测试结果Controller
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Api(tags = "水泵测试结果")
@RestController
@RequestMapping("/shbData/all")
public class DataShbController extends BaseController
{
    @Autowired
    private IDataShbService dataShbService;


    /**
     * 查询水泵测试结果列表
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataShb dataShb)
    {
        startPage();
        List<DataShb> list = dataShbService.selectDataShbList(dataShb);
        return getDataTable(list);
    }

    /**
     * 导出水泵测试结果列表
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:export')")
    @Log(title = "水泵测试结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataShb dataShb)
    {
        List<DataShb> list = dataShbService.selectDataShbList(dataShb);
        ExcelUtil<DataShb> util = new ExcelUtil<>(DataShb.class);
        util.exportExcel(response, list, "水泵测试结果数据");
    }

    /**
     * 获取水泵测试结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:query')")
    @GetMapping(value = "/{shbId}")
    public AjaxResult getInfo(@PathVariable("shbId") Long shbId)
    {
        return success(dataShbService.selectDataShbByShbId(shbId));
    }

    /**
     * 获取水泵测试结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:query')")
    @GetMapping(value = "/getInfoByDataTaskId/{dataTaskId}")
    @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long")
    @ApiOperation(value="获取水泵测试结果详细信息")
    public AjaxResult getInfoByDataTaskId(@PathVariable("dataTaskId") Long dataTaskId) throws ExecutionException, InterruptedException {
        return success(dataShbService.getShbDetailTask(dataTaskId));
    }

    /**
     * 新增水泵测试结果
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:add')")
    @Log(title = "水泵测试结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataShb dataShb)
    {
        return toAjax(dataShbService.insertDataShb(dataShb));
    }

    /**
     * 修改水泵测试结果
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:edit')")
    @Log(title = "水泵测试结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataShb dataShb)
    {
        return toAjax(dataShbService.updateDataShb(dataShb));
    }


    /**
     * 根据任务id删除水泵测试结果
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:remove')")
    @Log(title = "根据任务id删除水泵测试结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{dataTaskId}/{shbId}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "shbId", value = "水泵id", required = true, dataType = "Long"),
    })
    @ApiOperation(value="根据任务id删除水泵测试结果")
    public AjaxResult remove(@PathVariable("dataTaskId")Long dataTaskId, @PathVariable("shbId")Long shbId)
    {
        return toAjax(dataShbService.updateDataShbByHjId(dataTaskId, shbId));
    }

    /**
     * 根据任务id删除整条水泵测试结果
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:remove')")
    @Log(title = "根据任务id删除整条水泵测试结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/updateDataShbByDataTaskId/{dataTaskId}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiOperation(value="根据任务id删除整条水泵测试结果")
    public AjaxResult updateDataShbByDataTaskId(@PathVariable("dataTaskId")Long dataTaskId)
    {
        return toAjax(dataShbService.updateDataShbByDataTaskId(dataTaskId));
    }

    /**
     * 删除水泵测试结果
     */
    @PreAuthorize("@ss.hasPermi('data:shuibeng:remove')")
    @Log(title = "水泵测试结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shbIds}")
    public AjaxResult remove(@PathVariable Long[] shbIds)
    {
        return toAjax(dataShbService.deleteDataShbByShbIds(shbIds));
    }

    /**
     * 泵效图所需的数据
     *
     * @param shbIds 测试数据ID
     * @return
     */
    @ApiOperation("泵效图数据")
    @PreAuthorize("@ss.hasPermi('data:shuibeng:query')")
    @GetMapping("/task/chart/{shbIds}")
    public AjaxResult getChartElements(@PathVariable Long[] shbIds) {
        ShbChartRespDTO chartData = dataShbService.getChartData(shbIds);
        return chartData != null ? AjaxResult.success(chartData) : AjaxResult.error();
    }

    /**
     * 重新计算
     *
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("重新计算单条测试数据")
    @PreAuthorize("@ss.hasPermi('data:shuibeng:edit')")
    @PostMapping("/data/modify")
    public AjaxResult modify(@RequestBody DataShbModifyReqDTO dataShbModifyReqDTO) {
        return AjaxResult.success(dataShbService.modify(dataShbModifyReqDTO));
    }

    /**
     * 保存的水泵数据导入报告
     */
    @PreAuthorize("@ss.hasPermi('sbData:import:query')")
    @PostMapping("/dataShbImportReport")
    @ApiOperation(value="保存的水泵数据导入报告")
    public AjaxResult dataShbImportReport(@ApiParam(name="测试项目结果数据",value="传入json格式",required=true)@RequestBody ShbReportReqDTO shbReportReqDTO){
        return AjaxResult.success(dataShbService.dataShbImportReport(shbReportReqDTO));
    }
}
